09-08-2021
// Instead of confusing React with code like this:
if (userName !== '') {
useEffect(() => {
localStorage.setItem('savedUserName', userName);
});
}
// We can accomplish the same goal, while consistently calling our Hook every time:
useEffect(() => {
if (userName !== '') {
localStorage.setItem('savedUserName', userName);
}
});
const [color, setColor] = useState();
當前狀態(current state) - 範例中的color
更新狀態的函數(state setter) - 範例中的setColor
import React, { useState } from 'react';
export default function ColorPicker() {
const [color, setColor] = useState();
const divStyle = {backgroundColor: color};
return (
<div style={divStyle}>
<p>The color is {color}</p>
<button onClick={() => setColor('Aquamarine')}>
Aquamarine
</button>
<button onClick={() => setColor('BlueViolet')}>
BlueViolet
</button>
<button onClick={() => setColor('Chartreuse')}>
Chartreuse
</button>
<button onClick={() => setColor('CornflowerBlue')}>
CornflowerBlue
</button>
</div>
);
}
import React, { useState } from 'react';
export default function ColorPicker() {
const [color, setColor] = useState();
const [color, setColor] = useState();
import React, { useState } from 'react';
const colorNames = ['Aquamarine', 'BlueViolet', 'Chartreuse', 'CornflowerBlue', 'Thistle', 'SpringGreen', 'SaddleBrown', 'PapayaWhip', 'MistyRose'];
export default function ColorPicker() {
const [color, setColor] = useState("Tomato");
const divStyle = {backgroundColor: color};
return (
<div style={divStyle}>
<p>Selected color: {color}</p>
{colorNames.map((colorName)=>(
<button
onClick={() => setColor(colorName)}
key={colorName}>
{colorName}
</button>
))}
</div>
);
}
在初次頁面使用 "Tomato" 作為初始化的頁面顏色
export default function ColorPicker() {
const [color, setColor] = useState("Tomato");
const divStyle = {backgroundColor: color};
常見的模式
const handleChange = (event) => {
const newEmail = event.target.value;
setEmail(newEmail);
}
也可以這樣簡化
const handleChange = (event) => setEmail(event.target.value);
或是使用解構的方式
const handleChange = ({target}) => setEmail(target.value);
範例:
import React, { useState } from "react";
// regex to match numbers between 1 and 10 digits long
const validPhoneNumber = /^\d{1,10}$/;
export default function PhoneNumber() {
const [phone, setPhone] = useState('');
const handleChange = ({ target })=> {
const newPhone = target.value;
const isValid = validPhoneNumber.test(newPhone);
if (isValid) {
setPhone(newPhone);
}
// just ignore the event, when new value is invalid
};
return (
<div className='phone'>
<label for='phone-input'>Phone: </label>
<input value={phone} onChange={handleChange} id='phone-input' />
</div>
);
}
在value 有更動的時候會去執行onChang{handleChange}
<input value={phone} onChange={handleChange} id='phone-input' />
</div>
import React, { useState } from 'react';
export default function QuizNavBar({ questions }) {
const [questionIndex, setQuestionIndex] = useState(0);
const goBack = () =>
setQuestionIndex((prevQuestionIndex) => prevQuestionIndex - 1);
const goToNext = () =>
setQuestionIndex((prevQuestionIndex) => prevQuestionIndex + 1);
const onFirstQuestion = questionIndex === 0;
// 起始值是0
const onLastQuestion = questionIndex === questions.length - 1;
// 回到上一個問題,等於問題-1
return (
<nav>
<span>Question #{questionIndex + 1}</span>
<div>
<button onClick={goBack} disabled={onFirstQuestion}>
// 點擊上一步,或是在等於0的時候無法使用
Go Back
</button>
<button onClick={goToNext} disabled={onLastQuestion}>
// 點擊往下一步,或是在最後一個無法使用
Next Question
</button>
</div>
</nav>
);
}